
/*

 Program: 
	
 - This program runs the regressions to compute the idiosyncratic, sector, and macro 
   components of total, domestic or bilateral export sales growth. Following Koren and Tenreyro (QJE2007) 
   we will run a regression for each year individually, as well as each destination market.

 - The constant in all regressions will reflect the "aggregate" component. Thus, we have
   issues of perfect collinearity if we include all sector-destination effects. For transparancy,
   we follow the approach from Stockman (JME1988) and place some explicit constraints on the coefficients. 

   In this case, we need to constrain one sector dummy to be zero ("normalization"). 
   This cannot be done with the xtreg command, so we will create fixed effects using dummies, 
   and constrain a given sector to be 0 throughout. 
			
	`1': (demeaned) growth variable
        `2': sector variable
        `3': year to keep
        `4': sector number to constrain
*/

capture program drop dGLM_2_growth_regs
program define dGLM_2_growth_regs

keep if year==`3'
local minobs = 10

   /***********************************************************************************
   
   APPROACH IN A NUTSHELL:
   
   - Regress firm-destination level growth rates on sectoral dummies (with one "reference" sector)
   - Coefficients on sectors are "sectoral" level shocks
   - Coefficient on constant is "aggregate" shock
                
   - This approach is done for each year
   - Results will be collected from second year onwards as growth rates are undefined for first year
   
   ************************************************************************************/

   *** CONSTRUCTION OF SECTOR DUMMIES ***
   
   * Sufficient condition(s) for constructing shocks:
   *	- at least 10 observations per destination/year pair
   
   qui count
   local nobs = r(N)
   
   if `nobs'>`minobs'  {
   
	* Create dummies and set up constrained sector, needed given that SIC doesn't increase uniformly
	* "refsect" has to be the last one => replace
	egen sic_num = group(`2')
	quietly sum sic_num if `2'=="`4'"
	local sc = r(min)
	* Generate dummy variables under the variables "sec"
	quietly tab sic_num, ge(sec)

	* Define first constraint: "normalization" of reference sector 
	constraint 1 sec`sc' = 0

	*** CONSTRUCTION OF IDIOSYNCRATIC SHOCKS ***
   
	* Regression subject to first constraint
	* - "Demeaning" at sectoral level
	quietly cnsreg `1' sec*, c(1)

	* Obtain idiosyncratic shocks
	* - Residuals from previous regression
	* - Idiosyncratic shocks are basically demeaned (at sectoral level) firm-level growth rates
	predict gr_idio, res
   
	* Validity of fixed effects approach (e.g. see Amiti-Weinstein (2013))
	* - Generate fitted values
	* - Correlate fitted values with actual values
	* - Generate R-squared
	predict `1'_hat
	corr `1' `1'_hat if e(sample) 
	ge Rsquared=r(rho)^2

	*** CONSTRUCTION OF SECTOR LEVEL SHOCKS *** 

	* Store coefficients on sectors ánd constant in vector "b"
	mat b = e(b)
	* Store coefficients on sectors only in vector "b_sec"
	mat b_sec = b[1,1..(colsof(b)-1)]	
   
	* Create variable with sectoral level shock "gr_sec"
	* - Each firm-destination level growth rate will have a corresponding sectoral level shock
	* - The exceptions are, of course, those firm-destination level observations belonging to "reference" sector
	gen gr_sec = 0
	local cb = colsof(b)-1
	quietly foreach X of numlist 1/`cb'{
			replace gr_sec = b_sec[1,`X'] if sec`X'==1
	}

	* Correlation matrix between firm-destination level growth rates and sectoral shocks
	corr `1' gr_sec if e(sample) 
	ge Rspart_sec=r(rho)^2

	* Adjusted R-squared
	quietly sum `1' if e(sample)
	local count=r(N)
	ge countreg=colsof(b)
	quietly sum countreg
	local countreg=r(mean)
	ge Rbarsquared=1-(1-Rsquared)*(`count'-1)/(`count'-`countreg'-1)
	
	*** CONSTRUCTION OF MACRO (AGGREGATE) SHOCKS *** 

	* Macro-level shock is simply the constant ("common component") of the previous regression
	ge gr_macro = b[1,colsof(b)]
   
	* Correlation matrix between firm-destination level growth rates and macro shock
	corr `1' gr_macro if e(sample) 
	ge Rspart_macro=r(rho)^2
	* Note: by construction, this should be zero as aggregate shock is a constant.
	
   }
   
   else  {
   
	ge gr_idio=0
	ge gr_sec=0
	ge gr_macro=0
	ge Rsquared=0
	ge Rbarsquared=0
	ge Rspart_sec=0
	ge Rspart_macro=0
	ge `1'_hat=0

   }
	
   keep firmid id* `2' year `1' revctry country iso* w_fn gr_idio gr_sec gr_macro Rsquared Rbarsquared Rspart_sec Rspart_macro `1'_hat
   * Note: id* contains
   * - "id_c": country identifier
   * - "id_fn": firm-country (or firm-destination) identifier
   
   compress
	
* End of .do file	
end
